package xin.yangshuai.juc01.hello;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Question10
 *
 * @author shuai
 * @date 2022/3/7
 */
public class Question10 {
    private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();

    ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();


    public void readToWrite() {

        readLock.lock();
        System.out.println("read lock");

        writeLock.lock();
        System.out.println("write lock");

        readLock.unlock();
        System.out.println("read unlock");

        writeLock.unlock();
        System.out.println("write unlock");



    }

    public void writeToRead() {

        writeLock.lock();
        System.out.println("write lock");

        readLock.lock();
        System.out.println("read lock");

        writeLock.unlock();
        System.out.println("write unlock");

        readLock.unlock();
        System.out.println("read unlock");

    }

}

class Result10{
    public static void main(String[] args) {

        Question10 question10 = new Question10();
        question10.writeToRead();

    }
}
